package com.example.tool.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.tool.R;
import com.example.tool.mvp.base.BasePresenter;
import com.example.tool.mvp.base.IBaseView;
import com.example.tool.util.DebugLog;
import com.gyf.immersionbar.ImmersionBar;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Create by liangkun on 2018/11/22
 * Describe :mvp_fragment基类
 */
public abstract class BaseMvpFragment<P extends BasePresenter> extends Fragment implements IBaseView {
    protected P presenter;
    private View rootView;
    private Unbinder unBinder;
    public Context mContext;
    private View toolbar;
    /**
     * rootView是否初始化标志，防止回调函数在rootView为空的时候触发
     */
    private boolean hasCreateView;
    /**
     * 视图是否已经初初始化
     */
    protected boolean isInit = false;
    protected boolean isLoad = false;
    /**
     * 当前Fragment是否处于可见状态标志，防止因ViewPager的缓存机制而导致回调函数的触发
     */
    private boolean isFragmentVisible;

    protected BaseMvpFragment() {
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        //创建present
        presenter = createPresenter();
        if (presenter != null) {
            presenter.attachView(this);
        }
        initVariable();
    }

    /**
     * 初始化参数
     */
    private void initVariable() {
        hasCreateView = false;
        isFragmentVisible = false;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(getLayoutId(), container, false);
        }
        unBinder = ButterKnife.bind(this, rootView);
        //toolbar
        toolbar = rootView.findViewById(R.id.toolbar);
        DebugLog.i(DebugLog.KUN +"onCreateView: "+toolbar);

        fitsLayoutOverlap();
        isInit = true;
        //初始化的时候去加载数据
        isCanLoadData();
        initData();
        initView();
        initEvent();
        return rootView;
    }

    /**
     * 是否可以加载数据
     * 可以加载数据的条件：
     * 1.视图已经初始化
     * 2.视图对用户可见
     */
    private void isCanLoadData() {
        if (!isInit) {
            return;
        }

        if (getUserVisibleHint()) {
            lazyLoad();
            isLoad = true;
        } else {
            if (isLoad) {
                stopLoad();
            }
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isCanLoadData();
//        DebugLog.d(DebugLog.LOGPHONE + " BaseMvpFragment ------setUserVisibleHint- " + isVisibleToUser);
        if (rootView == null) {
            //没有创建，正常走
            return;
        }
        hasCreateView = true;
        //可见
        if (isVisibleToUser) {
            //子类复写方法
            onFragmentVisibleChange(true);
            isFragmentVisible = true;
            return;
        }
        //fragment不不见了
        if (isFragmentVisible) {
            onFragmentVisibleChange(false);
            isFragmentVisible = false;
        }
    }

    /**
     * 视图销毁的时候讲Fragment是否初始化的状态变为false
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isInit = false;
        isLoad = false;

    }

    /**
     * 当视图初始化并且对用户可见的时候去真正的加载数据
     */
    protected abstract void lazyLoad();

    /**
     * 当视图已经对用户不可见并且加载过数据，如果需要在切换到其他页面时停止加载数据，可以覆写此方法
     */
    protected void stopLoad() {
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!hasCreateView && getUserVisibleHint()) {
//            DebugLog.d(DebugLog.LOGPHONE + " BaseMvpFragment ------onViewCreated- ");
            onFragmentVisibleChange(true);
            isFragmentVisible = true;
        }
    }

    protected abstract void initEvent();

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void initData();

    /**
     * 当前fragment可见状态发生变化时会回调该方法
     * 如果当前fragment是第一次加载，等待onCreateView后才会回调该方法，其它情况回调时机跟 {@link #setUserVisibleHint(boolean)}一致
     * 在该回调方法中你可以做一些加载数据操作，甚至是控件的操作，因为配合fragment的view复用机制，你不用担心在对控件操作中会报 null 异常
     *
     * @param isVisible true  不可见 -> 可见
     *                  false 可见  -> 不可见
     */
    protected void onFragmentVisibleChange(boolean isVisible) {

    }

    /**
     * *************************************IBaseView方法实现*************************************
     */




    protected abstract P createPresenter();

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
        }
        unBinder.unbind();

    }


    /**
     * 填充
     */
    private void fitsLayoutOverlap() {
        ImmersionBar.setTitleBar(this, toolbar);
    }

}
